package com.hopu.day6_springboot.config;

import com.hopu.day6_springboot.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * 拦截器配置
 */
@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {

    @Bean
    public HandlerInterceptor getLoginInterceptor(){
        return new LoginInterceptor();
    }

    @Autowired
    private LoginInterceptor loginInterceptor;

    /**
     * 添加拦截器
     * @param registry
     *
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //添加登录拦截器
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**") //配置拦截路径
                .excludePathPatterns("/**/login","/**/login.html","/**/code.do",
                        "/**/*.js","/**/*.css"); //配置放行路径
        super.addInterceptors(registry);
    }

    /**
     * 添加静态资源的处理器
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:public")
                .addResourceLocations("classpath:static")
                .addResourceLocations("classpath:resources");
        super.addResourceHandlers(registry);
    }
}
